package com.example.AOPTest.proxy.CGlibTest;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * description: MyMethodInterceptor <br>
 * date: 2021/4/23 15:41 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class MyMethodInterceptor implements MethodInterceptor {

    private Object target;

    public Object getInstance(Object target){
        this.target = target;

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        // 处理的创建过程
        Object proxyObj = enhancer.create() ;
        return proxyObj;
    }

    @Override
    public Object intercept(Object proxy, Method method , Object[] args, MethodProxy methodProxy) throws InvocationTargetException, IllegalAccessException {
        Object object = null;
        System.out.println("-------------------cglib Start");
        object = method.invoke(target, args);
        System.out.println("-------------------cglib End");
        return object;
    }

}
